package com.yanqu.road.dao.dbV2.serializer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yanqu.road.utils.string.StringUtils;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Helper {
    private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static LocalDateTime getDbZeroTime() {
        return LocalDateTime.parse("2000-01-01 00:00:00", dtf);
    }

    public static String toJSONString(Object o) {
        String s = "";
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            s = objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException ignored) {
        }
        return s;
    }

    public static <T> T toObject(String s, TypeReference valueTypeRef) {
        if (StringUtils.isNullOrEmpty(s)) {
            return null;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            return objectMapper.readValue(s, valueTypeRef);
        } catch (IOException ignored) {

        }
        return null;
    }
}
